package com.apes.fn.server.servicePlan.repository;

import com.apes.fn.server.servicePlan.model.ClientInvoiceRules;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ClientInvoiceRulesRepository extends CustomRepository<ClientInvoiceRules,  String> {

    @Query("select t from ClientInvoiceRules t left join t.clientInvoiceRulesItems i " +
            "where t.customer.id = :customerId and i.dept.id = :deptId")
    ClientInvoiceRules findClientInvoiceRulesByItemDept(@Param("deptId") String deptId, @Param("customerId") String customerId);

    @Query("select a from ClientInvoiceRules a ,ClientInvoiceRulesItem b where a.id = b.clientInvoiceRules " +
            " and (a.customer.id like %:id% or a.customer.party.name like %:name%)" +
            " and b.dept.id = :deptId")
    Page<ClientInvoiceRules> findByUnitClient(@Param("id") String id, @Param("name") String name, @Param("deptId") String deptId, Pageable pageable);
}
